package com.ourteam.product.api;

import java.util.Date;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.IProductUIFormFieldDAO;
import com.ourteam.product.ui.dao.ProductUIFormDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIFormFieldDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIFormBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldBean;
import com.ourteam.product.ui.domain.ProductUIFormLayoutDataBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.form.FormBean;
import net.ui.model.form.FormPropertyBean;
import net.ui.model.form.LayoutDataBean;

@Controller
@RequestMapping("/api/PRODUCT/UI/productUIForm")
public class ProductUIFormAPI
		extends AbstractCRUDServiceController<ProductUIFormDAOQueryBean, ProductUIFormSearchModel, ProductUIFormBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIFormDAOQueryBean newQueryBean() {
		return new ProductUIFormDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIFormDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIFormCount(queryBean);
	}

	@Override
	protected ProductUIFormBean[] queryData(ProductUIFormDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIForms(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUIForms(ids);
	}

	@Override
	protected ProductUIFormBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIFormById(id);
	}

	@Override
	protected void saveData(ProductUIFormBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIForm(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIForm(dataBean);
		}
	}

	@RequestMapping("/getForm")
	@ResponseBody
	public FormBean getForm(@RequestParam long id) throws Exception {

		ProductUIFormBean productUIFormBean = PRODUCT_UI_SERVICE.getProductUIFormById(id);

		ProductUIFormFieldDAOQueryBean formFieldQueryBean = new ProductUIFormFieldDAOQueryBean();

		FormBean formBean = new FormBean();

		formBean.setId(String.valueOf(productUIFormBean.getId()));

		formBean.setTitle(productUIFormBean.getTitle());

		formBean.setTitleKey(productUIFormBean.getTitleKey());

		formBean.setNumColumns(String.valueOf(productUIFormBean.getNumColumns()));

		formBean.setDesc(productUIFormBean.getRemarks());

		formFieldQueryBean.clear();

		formFieldQueryBean.createCriteria().andFormIdEqualTo(productUIFormBean.getId()).andParentIdEqualTo(0);

		formFieldQueryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);

		ProductUIFormFieldBean[] formFieldBeans = PRODUCT_UI_SERVICE.queryProductUIFormFields(formFieldQueryBean);

		for (int j = 0; j < formFieldBeans.length; j++) {

			ProductUIFormFieldBean productUIFormFieldBean = formFieldBeans[j];

			FormPropertyBean formPropertyBean = new FormPropertyBean();

			formPropertyBean.setAllowNull(BooleanUtils.toBooleanObject(productUIFormFieldBean.getAllowEmpty()));

			formPropertyBean.setDataSrc(productUIFormFieldBean.getDataSource());

			formPropertyBean.setDesc(productUIFormFieldBean.getRemarks());

			formPropertyBean.setId(productUIFormFieldBean.getCode());

			formPropertyBean.setReadOnly(BooleanUtils.toBoolean(productUIFormFieldBean.getReadOnly()));

			formPropertyBean.setStyleDesc(productUIFormFieldBean.getStyle());

			formPropertyBean.setTipInfo(productUIFormFieldBean.getTipInfo());

			formPropertyBean.setTitle(productUIFormFieldBean.getTitle());

			formPropertyBean.setTitleKey(productUIFormFieldBean.getTitleKey());

			formPropertyBean.setType(productUIFormFieldBean.getTypeWidget());

			formPropertyBean.setValue(productUIFormFieldBean.getParameterValue());

			ProductUIFormLayoutDataBean[] layoutDataBeans = PRODUCT_UI_SERVICE
					.getProductUIFormFieldLayoutDatas(productUIFormFieldBean.getId());

			for (int k = 0; k < layoutDataBeans.length; k++) {

				ProductUIFormLayoutDataBean productUIFormLayoutDataBean = layoutDataBeans[k];

				LayoutDataBean layoutDataBean = new LayoutDataBean();

				layoutDataBean.setName(productUIFormLayoutDataBean.getName());

				layoutDataBean.setValue(productUIFormLayoutDataBean.getValue());

				formPropertyBean.addLayoutData(layoutDataBean);

			}

			if (BooleanUtils.toBoolean(productUIFormFieldBean.getAllowSubField())) {
				loadSunFormFields(formPropertyBean, productUIFormFieldBean);
			}

			formBean.addFormProperty(formPropertyBean);
		}

		return formBean;
	}

	protected void loadSunFormFields(FormPropertyBean formPropertyBean, ProductUIFormFieldBean productUIFormFieldBean)
			throws Exception {

		ProductUIFormFieldDAOQueryBean formFieldQueryBean = new ProductUIFormFieldDAOQueryBean();

		formFieldQueryBean.clear();

		formFieldQueryBean.createCriteria().andFormIdEqualTo(productUIFormFieldBean.getFormId())
				.andParentIdEqualTo(productUIFormFieldBean.getId());

		formFieldQueryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);

		ProductUIFormFieldBean[] formFieldBeans = PRODUCT_UI_SERVICE.queryProductUIFormFields(formFieldQueryBean);

		for (int i = 0; i < formFieldBeans.length; i++) {

			ProductUIFormFieldBean subProductUIFormFieldBean = formFieldBeans[i];

			FormPropertyBean subFormPropertyBean = new FormPropertyBean();

			subFormPropertyBean.setAllowNull(BooleanUtils.toBooleanObject(subProductUIFormFieldBean.getAllowEmpty()));

			subFormPropertyBean.setDataSrc(subProductUIFormFieldBean.getDataSource());

			subFormPropertyBean.setDesc(subProductUIFormFieldBean.getRemarks());

			subFormPropertyBean.setId(subProductUIFormFieldBean.getCode());

			subFormPropertyBean.setReadOnly(BooleanUtils.toBoolean(subProductUIFormFieldBean.getReadOnly()));

			subFormPropertyBean.setStyleDesc(subProductUIFormFieldBean.getStyle());

			subFormPropertyBean.setTipInfo(subProductUIFormFieldBean.getTipInfo());

			subFormPropertyBean.setTitle(subProductUIFormFieldBean.getTitle());

			subFormPropertyBean.setTitleKey(subProductUIFormFieldBean.getTitleKey());

			subFormPropertyBean.setType(subProductUIFormFieldBean.getTypeWidget());

			subFormPropertyBean.setValue(subProductUIFormFieldBean.getParameterValue());

			ProductUIFormLayoutDataBean[] layoutDataBeans = PRODUCT_UI_SERVICE
					.getProductUIFormFieldLayoutDatas(subProductUIFormFieldBean.getId());

			for (int k = 0; k < layoutDataBeans.length; k++) {

				ProductUIFormLayoutDataBean productUIFormLayoutDataBean = layoutDataBeans[k];

				LayoutDataBean layoutDataBean = new LayoutDataBean();

				layoutDataBean.setName(productUIFormLayoutDataBean.getName());

				layoutDataBean.setValue(productUIFormLayoutDataBean.getValue());

				subFormPropertyBean.addLayoutData(layoutDataBean);

			}

			if (BooleanUtils.toBoolean(subProductUIFormFieldBean.getAllowSubField())) {
				loadSunFormFields(subFormPropertyBean, subProductUIFormFieldBean);
			}

			formPropertyBean.addSubProperty(subFormPropertyBean);
		}
	}

}
